package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.ProjectBiz;
import com.useeinfo.asg.modules.logging.entity.Project;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by THINK on 2017/4/27.
 */
@Controller
@RequestMapping("/admin/project")
public class ProjectAdminManageAction {

    @Autowired
    private ProjectBiz projectBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/admin/project/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(HttpServletRequest request,
                               @RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "projectName", required = false) String projectNameParam,
                               @RequestParam(value = "proManager", required = false) String proManagerParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        if (!CommUtil.null2String(projectNameParam).equals("")) {
            queryParam.getSqlMap().put("projectName", projectNameParam);
        }
        if (!CommUtil.null2String(proManagerParam).equals("")) {
            queryParam.getSqlMap().put("proManager", proManagerParam);
        }

        return projectBiz.findJSONList(queryParam);
    }

    @RequestMapping("/add")
    @ResponseBody
    public JSONObject add(HttpServletRequest request,Project project,String publishTime1 ,String nextPublishTime1) {
        project.setCreateTime(new Date());
        project.setNextPublishTime(CommUtil.formatDate(nextPublishTime1));
        project.setPublishTime(CommUtil.formatDate(publishTime1));
        if(project.getId()!=null){
            Project project1 = projectBiz.findModel(project.getId());
            project.setCreateTime(project1==null?new Date():project1.getCreateTime());
        }
        projectBiz.addOrUpdate(project);
        return CommUtil.jsonResult(true);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public JSONObject edit(HttpServletRequest request) {


        return null;
    }

    @RequestMapping(value = "/editPage", produces = "text/html;charset=UTF-8")
    public String edit(HttpServletRequest request, ModelMap mv,String id) {
        Project project = projectBiz.findModel(CommUtil.null2Long(id));
        mv.put("project",project);
        return "/admin/project/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request, ModelMap mv) {

        JSONObject retJson = CommUtil.jsonResult(false);
        try {
            long id = CommUtil.null2Long(request.getParameter("id"));
            Project project = null;
            if (id > 0 && (project = projectBiz.findModel(id)) != null) {
                project.setDeleteStatus(true);
                projectBiz.update(project);
                retJson = CommUtil.jsonResult(true);
            }else {
                retJson.put("msg", "未找到相应记录");
            }
        }
        catch (Exception e) {
            retJson.put("msg", "删除记录时发生错误");
        }
        return retJson;
    }
}
